package edu.sjb.characters;

import edu.sjb.Drawable;
import edu.sjb.constants.ImageConstants;
import edu.sjb.gui.GameScreen;
import edu.sjb.map.prizes.JumpingMushroom;
import edu.sjb.map.prizes.PowerUp;

public class Mario extends GameCharacter {
	protected boolean stillRight;
	protected boolean stillLeft;
	protected boolean stillDead;
	protected boolean walkingRight;
	protected boolean walkingLeft;
	protected boolean jumpingRight;
	protected boolean jumpingLeft;
	protected boolean runningRight;
	protected boolean runningLeft;
	protected PowerUp jumpItem;
	protected PowerUp powerUpItem;
	protected Thread jumpThread;
	protected boolean underMushroomEffect;
	protected boolean underSuperMushroomEffect;
	protected boolean underStarEffect;
	protected boolean underFlowerEffect;

	public Mario() {
		super();
	}

	public void action1() {
		if ((!underMushroomEffect) && !(jumpThread.isAlive())
		        && !(gravity().onAir())) {
			underMushroomEffect = true;
			Thread jt = new Thread(jumpItem);
			jt.start();
		}

	}

	public void action2() {
		Thread power = new Thread(powerUpItem);
		power.start();
	}

	@Override
	public void characterCollide(GameCharacter afectado) {
	}

	@Override
	public void hitAgainstMario(Mario elMario) {
	}

	@Override
	public void die() {
		if (!game().isMarioDead()) {
			deadStatus();
			game().marioDied();
		}
	}

	public void dash() {
		if (speedX == 20) {
			for (int i = 0; i < game().getCharacters().size(); i++) {
				game().getCharacters().get(i).setSpeedX(35);
			}
			for (int i = 0; i < game().getObjects().size(); i++) {
				game().getObjects().get(i).setSpeedX(35);
			}
			for (int i = 0; i < game().getPowerUps().size(); i++) {
				game().getPowerUps().get(i).setSpeedX(35);
			}
			if (walkingRight) {
				runRight();
			}
			else {
				if (walkingLeft) {
					runLeft();
				}
			}
		}
		else {
			speedX = 20;
			for (int i = 0; i < game().getCharacters().size(); i++) {
				game().getCharacters().get(i).setSpeedX(20);
			}
			for (int i = 0; i < game().getObjects().size(); i++) {
				game().getObjects().get(i).setSpeedX(20);
			}
			for (int i = 0; i < game().getPowerUps().size(); i++) {
				game().getPowerUps().get(i).setSpeedX(20);
			}
			if (runningRight) {
				stopRight();
			}
			else {
				stopLeft();
			}
		}
	}

	public void goStart() {

		if (underSuperMushroomEffect) {
			image.changeImage(ImageConstants.SUPERMARIO_STILL_R);
		}
		else {
			image.changeImage(ImageConstants.MARIOSMALL_STILL_R);
		}
	}

	public void stopRight() {

		if (((walkingRight) || (jumpingRight)) && (!stillDead)) {
			if (underSuperMushroomEffect) {
				image.changeImage(ImageConstants.SUPERMARIO_STILL_R);
			}
			else {
				image.changeImage(ImageConstants.MARIOSMALL_STILL_R);
			}
			walkingRight = false;
			jumpingRight = false;
			stillRight = true;
			walkingLeft = false;
			jumpingLeft = false;
			stillLeft = false;
		}
	}

	public void stopLeft() {
		if (((walkingLeft) || (jumpingLeft)) && (!stillDead)) {
			if (underSuperMushroomEffect) {
				image.changeImage(ImageConstants.SUPERMARIO_STILL_L);
			}
			else {
				image.changeImage(ImageConstants.MARIOSMALL_STILL_L);
			}
			walkingLeft = false;
			jumpingLeft = false;
			stillLeft = true;
			walkingRight = false;
			jumpingRight = false;
			stillRight = false;
		}
	}

	public void walkRight() {
		if (!jumpingRight || stillRight) {
			if (underSuperMushroomEffect) {
				image.changeImage(ImageConstants.SUPERMARIO_WALK_R);
			}
			else {
				image.changeImage(ImageConstants.MARIOSMALL_WALK_R);
			}
			walkingLeft = false;
			jumpingLeft = false;
			stillLeft = false;
			walkingRight = true;
			jumpingRight = false;
			stillRight = false;
		}
	}

	public void walkLeft() {
		if (!jumpingLeft || stillLeft) {
			if (underSuperMushroomEffect) {
				image.changeImage(ImageConstants.SUPERMARIO_WALK_L);
			}
			else {
				image.changeImage(ImageConstants.MARIOSMALL_WALK_L);
			}
			walkingLeft = true;
			jumpingLeft = false;
			stillLeft = false;
			walkingRight = false;
			jumpingRight = false;
			stillRight = false;

		}
	}

	public void runRight() {
		if (underSuperMushroomEffect) {
			image.changeImage(ImageConstants.SUPERMARIO_RUN_R);
		}
		else {
			image.changeImage(ImageConstants.MARIOSMALL_RUN_R);
		}
		runningRight = true;
	}

	public void runLeft() {
		if (underSuperMushroomEffect) {
			image.changeImage(ImageConstants.SUPERMARIO_RUN_L);
		}
		else {
			image.changeImage(ImageConstants.MARIOSMALL_RUN_L);
		}
		runningLeft = true;
	}

	public void deadStatus() {
		stillDead = true;
		image().changeImage(ImageConstants.MARIOSMALL_DEAD);
	}

	public void jump() {
		if ((walkingRight) || (stillRight)) {
			if (underSuperMushroomEffect) {
				image.changeImage(ImageConstants.SUPERMARIO_JUMP_R);
			}
			else {
				image.changeImage(ImageConstants.MARIOSMALL_JUMP_R);
			}
			walkingLeft = false;
			jumpingLeft = false;
			stillLeft = false;
			walkingRight = false;
			jumpingRight = true;
			stillRight = false;
		}
		else {
			if ((walkingLeft) || (stillLeft)) {
				if (underSuperMushroomEffect) {
					image.changeImage(ImageConstants.SUPERMARIO_JUMP_L);
				}
				else {
					image.changeImage(ImageConstants.MARIOSMALL_JUMP_L);
				}
				walkingLeft = false;
				jumpingLeft = true;
				stillLeft = false;
				walkingRight = false;
				jumpingRight = false;
				stillRight = false;
			}
		}
	}

	public void setUnderMushroomEffect(boolean m) {
		underMushroomEffect = m;
	}

	public void setUnderSuperMushroomEffect(boolean sm) {
		underSuperMushroomEffect = sm;
	}

	public void setUnderStarEffect(boolean s) {
		underStarEffect = s;
	}

	public void setUnderFlowerEffect(boolean f) {
		underFlowerEffect = f;
	}

	public void shrink() {
		game().mario().setSizeY(32);
		game().mario().setPosY(game().mario().posY() + 32);
		game().mario()
		        .image()
		        .setBounds(game().mario().posX(), game().mario().posY() + 32,
		                32, 32);
	}

	public void grow() {
		game().mario().setSizeY(64);
		game().mario().setPosY(game().mario().posY() - 32);
		game().mario()
		        .image()
		        .setBounds(game().mario().posX(), game().mario().posY() - 32,
		                32, 64);
	}

	public void setPowerUp(PowerUp pu) {
		powerUpItem = pu;
	}

	public void setGameContext(GameScreen gs) {
		jumpItem.setGameContext(gs);
		gameContext = gs;
	}

	private void setJumpItem(PowerUp jpu) {
		jumpItem = jpu;
	}

	private void setJumpThread(Thread hiloSaltar) {
		jumpThread = hiloSaltar;
	}

	@Override
	public void run() {
		for (int i = 0; i < 145; i++) {
			walkRight();
			setSpeedX(20);
			shiftPosX(10);
			try {
				Thread.sleep(20);
			}
			catch (InterruptedException e) {
			}
		}
		stopRight();
		game().removeEntity(this);
		game().finishGame();
	}

	public boolean underMushroomEffect() {
		return underMushroomEffect;
	}

	public boolean underSuperMushroomEffect() {
		return underSuperMushroomEffect;
	}

	public boolean underStarEffect() {
		return underStarEffect;
	}

	public boolean underFlowerEffect() {
		return underFlowerEffect;
	}

	protected Object clone() {
		Mario m = new Mario();
		JumpingMushroom jm = new JumpingMushroom();
		m.setUnderMushroomEffect(false);
		m.setJumpItem(jm);
		m.setJumpThread(new Thread(jumpItem));
		Drawable img = new Drawable(ImageConstants.MARIOSMALL_STILL_R, false);
		m.setImage(img);
		m.walkRight();
		return m;
	}
}
